/**
 * Created by Administrator on 2017/7/20.
 */
layui.use(['validator', 'ajaxRequest', 'form'], function (s) {

    var ajaxRequest = layui.ajaxRequest, form = layui.form();
    var controller = 'account';
    var validator = layui.validator;

    //获得添加页面
    $(document).on('click', '.add-user-account', function () {
        var url = '/' + controller + '/create';
        ajaxRequest.addGetHtml(url, '添加用户', 500, 600);
    });
    //添加数据
    $(document).on('click', '.add-user-sub', function () {
        form.on('submit(user-account-add)', function (data) {
            var field = data.field, url = '/' + controller;

            if (field.password_1 != field.password) {
                layer.tips('密码不一致','#password');
                return false;
            }
            ajaxRequest.ajaxRequest(url, field, 'json', 'post');
        });
        return false;
    });
    //删除数据
    $(document).on('click', '.oa-del-data', function () {
        var id = $(this).attr('data-id'), url = '/' + controller + '/' + id;
        ajaxRequest.mj_del(url, id, 'json', 'DELETE');
    });
    //编辑
    $(document).on('click', '.oa-edit-data', function () {
        var id = $(this).attr('data-id'), url = '/' + controller + '/' + id + '/edit';
        ajaxRequest.getHtml(url, id, '编辑用户', 500, 600);
    });
    //保存编辑数据
    $(document).on('click', '.edit-user-sub', function () {
        form.on('submit(user-account-edit)', function (data) {
            var id = $(this).attr('data-id'), field = data.field, url = '/' + controller + '/' + id;
            ajaxRequest.ajaxRequest(url, field, 'json', 'PUT');
        });
        return false;
    });
    //ajax 加载请求的selete下拉框
    form.on('select(depart)', function (data) {
        var id = data.value, url = _admin + 'Account/ajaxGetUserByDepartId';
        ajaxRequest.loadHtmlBySelect($('#leader'), url, {id: id});
    });
    //自定义验证规则
    form.verify({
        mobile: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (validator.IsMobilePhoneNumber(value) == false) {
                return '手机号格式不正确';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        , password: function (value, item) {
            if (CheckPassWord3(value) == false) {
                return '密码只能为数字加字母长度最少6位';
            }
        }
    });
});